from flask import Blueprint, jsonify, request
from models import Login, User

bp = Blueprint("login", __name__, url_prefix="/login")

@bp.route("/account", methods=["POST", "GET"])
def account():
    # 获取前端post请求发送来的数据(username,password)
    login_data = request.get_json()
    # login_data = {"username":"wujinfeng", "password":"123456"}
    print(login_data)
    isUser = User.query.filter(User.username == login_data["username"]).first()
    if isUser is None:
        return jsonify({"data": "no username"})
    else:
        # isPasswd = isUser.filter_by(password = login_data["password"]).first()
        isPasswd = (isUser.password == login_data["password"])
        if not isPasswd:
            return jsonify({"data": "fail passwd"})
        else:
            # return jsonify({"data": "success"})
            baseUserInfo = {
                "nickname": isUser.nickname,
                "email": isUser.email,
                "gender": isUser.gender,
                "userintro": isUser.userintro,
                "username": isUser.username,
                "money": isUser.money
                }
            print("baseUserInfo: ", baseUserInfo)
            return jsonify({"data": "success", "baseUserInfo": baseUserInfo})

@bp.route("/email_account", methods=["POST", "GET"])
def email_account():
    # 获取前端post请求发送来的数据(username,password)
    login_data = request.get_json()
    # login_data = {"username":"wujinfeng", "password":"123456"}
    print(login_data)
    isUser = User.query.filter(User.email == login_data["email"]).first()
    if isUser is None:
        return jsonify({"data": "no email"})
    else:
        isPasswd = (isUser.password == login_data["password"])
        if not isPasswd:
            return jsonify({"data": "fail passwd"})
        else:
            baseUserInfo = {
                "nickname": isUser.nickname,
                "email": isUser.email,
                "gender": isUser.gender,
                "userintro": isUser.userintro,
                "username": isUser.username,
                "money": isUser.money
                }
            print("baseUserInfo: ", baseUserInfo)
            return jsonify({"data": "success", "baseUserInfo": baseUserInfo})
    